package cn.edu.ujn.shi.controller;

import cn.edu.ujn.shi.dao.Dingdan;
import cn.edu.ujn.shi.dao.Msg;
import cn.edu.ujn.shi.service.IDingdanService;
import cn.edu.ujn.shi.service.IMessageService;
import jakarta.servlet.RequestDispatcher;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.PrintWriter;
import java.util.List;

/**
 * AListDingdanController
 *
 * @author clq
 * @version 2024/05/28 21:25
 **/
@Controller
public class DingdanController {
    @Autowired
    IDingdanService iDingdanService;

    @Autowired
    IMessageService messageService;

    @RequestMapping("/AListDingdanServlet")
    public void aListDingdanServlet(HttpServletRequest request, HttpServletResponse response) {

        try {
            response.setContentType("text/html;charset=utf-8");

            String username = request.getParameter("username");
            List<Dingdan> list = iDingdanService.getMyDingdanLike(username);

            PrintWriter out = response.getWriter();
            RequestDispatcher headerDispatcher = request.getRequestDispatcher("/WEB-INF/jsp/header-admin.jsp");
            headerDispatcher.include(request, response);
            out.println("<center><img src='images/aa5.png' height=70px><font size='+4' face='楷体' color='#7D26CD'><b>全部锁定信息管理</b></font></center>");
            out.println("<hr/>");

            out.println("<br/>");

            out.println("<center>");

            out.println("    <form action='AListDingdanServlet' method='get'>\n" +
                    "        <label for='username'>按用户名搜索：</label>\n" +
                    "        <input type='text' id='username' name='username'>\n" +
                    "        <button type='submit'>搜索</button>\n" +
                    "    </form>");
            out.println("<table width=95% border=1>");

            out.println("<tr bgcolor='#E0EEE0'>");
            out.println("<td align='center'>照片</td><td align='center'>失物</td><td align='center'>锁定用户</td><td align='center'>酬谢金额</td><td align='center'>发布账号</td><td align='center'>联系电话</td><td align='center'>操作</td>");
            out.println("</tr>");
            for (Dingdan g : list) {
                out.println("<tr>");
                out.println("<td align='center' width=200px>");
                out.println("<img src='images/pho/" + g.getPhoto() + ".jpg' width='200' height='180'/>");
                out.println("</td>");


                out.println("<td align='center'>");
                out.println(g.getName());
                out.println("</td>");
                //out.println("<td>");
                //out.println(g.getType());
                //out.println("</td>");
                out.println("<td align='center'>");
                out.println(g.getUsername());
                out.println("</td>");

                out.println("<td align='center'>");
                out.println(g.getMoney());
                out.println("</td>");
                out.println("<td align='center'>");
                out.println(g.getPeople());
                out.println("</td>");
                out.println("<td align='center'>");
                out.println(g.getTel());
                out.println("</td>");

                out.println("<td align='center'>");
                out.println("<a href='DeleteDingDanServlet?did=" + g.getDid() + "&&mid=" + g.getMid() + "'>取消锁定</a>");
                out.println("</td>");
                out.println("</tr>");
            }

            out.println("</table></center>");
            RequestDispatcher headerDispatcher1 = request.getRequestDispatcher("/WEB-INF/jsp/footer.jsp");
            headerDispatcher1.include(request, response);
            out.flush();
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @RequestMapping("/DeleteDingDanServlet")
    public void deleteDingDanServlet(HttpServletRequest request, HttpServletResponse response) {

        try {
            response.setContentType("text/html;charset=utf-8");
            request.setCharacterEncoding("utf-8");

            int did = Integer.parseInt(request.getParameter("did"));
            int mid = Integer.parseInt(request.getParameter("mid"));

            //�ͷ�fang
            messageService.updateAFlag(mid);
            //ɾ������
            iDingdanService.delete(did);

            response.sendRedirect("AListDingdanServlet");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @RequestMapping("/GoDingServlet")
    public void goDingServlet(HttpServletRequest request, HttpServletResponse response) {

        try {
            response.setContentType("text/html;charset=utf-8");
            request.setCharacterEncoding("utf-8");
            PrintWriter out = response.getWriter();

            HttpSession hs = request.getSession();
            //�õ��û���
            String username = hs.getAttribute("username").toString();

            int mid = Integer.parseInt(request.getParameter("mid"));

            List<Msg> mlist = messageService.getMessage(mid);

            out.println("<body bgcolor='#D7E8F6'>");

            out.println("<div style='width:100%;height:58px;background:#D7E8F6;border: 1px solid ;'>");
            out.println("<div style='margin-left:480px;margin-top:10px;'>");

            out.println("<a href='MainServlet'><img src='images/u1.png' width='40px'/><font color='red'><b>首页</b></font></a>&nbsp;");
            out.println("<a href='MyMessageServlet'><img src='images/u2.png' width='40px'/><font color='#000080'><b>个人信息</b></font></a>");
            out.println("<a href='AllServlet?search='><img src='images/u3.png' width='40px'/><font color='#000080'><b>全部信息</b></font></a>");
            out.println("<a href='MyDingdanServlet'><img src='images/u4.png' width='40px'/><font color='#000080'><b>个人锁定</b></font></a>");
            out.println("<a href='ListMyDiscussServlet'><img src='images/u5.png' width='40px'/><font color='#000080'><b>信息评价</b></font></a>");
            out.println("<a href='ListMyMsgServlet'><img src='images/u6.png' width='40px'/><font color='#000080'><b>我的信息</b></font></a>");
            out.println("<a href='index'><img src='images/u7.png' width='40px'/><font color='#000080'><b>退出</b></font></a>");
            out.println("</div>");
            out.println("</div>");

            out.println("<center><hr/><img src='images/top.jpg' width='100%' height='120px'><hr/></center>");

            out.println("<br/><br/>");
            out.println("<center><img src='images/u4.png' height=70px><font size='+4' face='楷体' color='#7D26CD'><b>信息锁定</b></font>");
            out.println("<hr/>");

            out.println("<center><font color='red' size=+3>请确认相关信息</font></center><br/><br/>");
            out.println("<center><font color='red' size=+2>确定后，信息状态变成锁定状态，请尽快联系信息发布人</font></center>");
            out.println("<center><table width='763' border='0' cellpadding='0' cellspacing='0' height='25' style='border-collapse: collapse' bordercolor='#111111'><tr>");
            out.println("<td height='25'></td></tr></table><br/>");


            out.println("<table><tr>");
            out.println("<td>");

            out.println("<b>物品名称：" + mlist.get(0).getName() + "</b><br/><br/>");
            out.println("<b>当前账号：" + username + "</b><br/><br/>");
            out.println("<b>承诺酬金:【" + mlist.get(0).getMoney() + "】元</b><br/><br/>");
            out.println("<b>发布账号：" + mlist.get(0).getPeople() + "</b><br/><br/>");
            out.println("<b>联系方式：" + mlist.get(0).getTel() + "</b><br/><br/>");

            out.println("<form action='InsertDingDanServlet?name=" + mlist.get(0).getName()
                    + "&&username=" + username + "&&photo=" + mlist.get(0).getPhoto()
                    + "&&money=" + mlist.get(0).getMoney() + "&&mid=" + mid
                    + "&&people=" + mlist.get(0).getPeople() + "&&tel=" + mlist.get(0).getTel() + "' method='post'>");
            out.println("</td>");
            out.println("</tr>");

            out.println("</table>");
            out.println("<input type='submit' value='确认锁定信息' style='width:320px;height:40px'/>");

            out.println("</form>");

            out.println("<br/><br/><hr/><img src='images/bottom.png' width='100%'>");
            out.println("</center><body>");
            out.flush();
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @RequestMapping("/InsertDingDanServlet")
    public void InsertDingDanServlet(HttpServletRequest request, HttpServletResponse response) {
        try {
            response.setContentType("text/html;charset=utf-8");
            request.setCharacterEncoding("utf-8");

            //��ǰ�û�
            HttpSession hs = request.getSession();
            String username = hs.getAttribute("username").toString();

            String name = request.getParameter("name");
            String photo = request.getParameter("photo");
            String money = request.getParameter("money");
            int mid = Integer.parseInt(request.getParameter("mid"));
            String people = request.getParameter("people");
            String tel = request.getParameter("tel");

            try {
                messageService.updateFlag(mid);
                iDingdanService.insert(new Dingdan(name, username, photo, money, mid, people, tel));
                response.sendRedirect("MyDingdanServlet");

            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @RequestMapping("/MyDingdanServlet")
    public void myDingdanServlet(HttpServletRequest request, HttpServletResponse response) {
        try {
            response.setContentType("text/html;charset=utf-8");
            request.setCharacterEncoding("utf-8");

            HttpSession hs = request.getSession();
            String username = hs.getAttribute("username").toString();

            List<Dingdan> list = iDingdanService.getMyDingdan(username);

            PrintWriter out = response.getWriter();
//            out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
//            out.println("<HTML>");
//            out.println("<HEAD><TITLE>我的订单</TITLE></HEAD>");
//
//            out.println("<body bgcolor='#D7E8F6'>");
//
//            out.println("<div style='width:100%;height:58px;background:#D7E8F6;border: 1px solid ;'>");
//            out.println("<div style='margin-left:480px;margin-top:10px;'>");
//
//            out.println("<a href='MainServlet'><img src='images/u1.png' width='40px'/><font color='red'><b>首页</b></font></a>&nbsp;");
//            out.println("<a href='MyMessageServlet'><img src='images/u2.png' width='40px'/><font color='#000080'><b>个人信息</b></font></a>");
//            out.println("<a href='AllServlet?search='><img src='images/u3.png' width='40px'/><font color='#000080'><b>全部信息</b></font></a>");
//            out.println("<a href='MyDingdanServlet'><img src='images/u4.png' width='40px'/><font color='#000080'><b>个人锁定</b></font></a>");
//            out.println("<a href='ListMyDiscussServlet'><img src='images/u5.png' width='40px'/><font color='#000080'><b>信息评价</b></font></a>");
//            out.println("<a href='ListMyMsgServlet'><img src='images/u6.png' width='40px'/><font color='#000080'><b>我的信息</b></font></a>");
//            out.println("<a href='index'><img src='images/u7.png' width='40px'/><font color='#000080'><b>退出</b></font></a>");
//            out.println("</div>");
//            out.println("</div>");

            RequestDispatcher headerDispatcher = request.getRequestDispatcher("/WEB-INF/jsp/header.jsp");
            headerDispatcher.include(request, response);

            out.println("<center><hr/><img src='images/top.jpg' width='100%' height=120px><hr/></center>");

            out.println("<br/>");
            out.println("<center><img src='images/u4.png' height=70px><font size='+4' face='楷体' color='#7D26CD'><b>个人锁定信息</b></font>");
            out.println("<hr/>");

            out.println("<center>");
            out.println("<br/><br/><table>");
            out.println("<tr>");

            out.println("<table width=95% border=1>");
            out.println("<tr bgcolor='#E0EEE0'>");
            out.println("<td align='center'>物品照片</td><td align='center'>失物</td><td align='center'>锁定用户</td><td align='center'>酬金</td><td align='center'>发布账号</td><td align='center'>联系电话</td><td align='center'>操作</td>");
            out.println("</tr>");
            for (Dingdan g : list) {
                out.println("<tr>");
                out.println("<td style='width:110px;height:150px'>");
                out.println("<img src='images/pho/" + g.getPhoto() + ".jpg' width='200px' height='200px'/>");
                out.println("</td>");

                //			out.println("<td>");
                //			out.println(g.getDid());
                //			out.println("</td>");

                //			out.println("<td>");
                //			out.println(mdao.getMessage(g.getMid()).get(0).getType());
                //			out.println("</td>");

                out.println("<td align='center'>");
                out.println(g.getName());
                out.println("</td>");
                out.println("<td align='center'>");
                out.println(g.getUsername());
                out.println("</td>");

                out.println("<td align='center'>");
                out.println(g.getMoney());
                out.println("</td>");
                out.println("<td align='center'>");
                out.println(g.getPeople());
                out.println("</td>");
                out.println("<td align='center'>");
                out.println(g.getTel());
                out.println("</td>");

                out.println("<td align='center'>");
                out.println("<a href='TuiServlet?did=" + g.getDid() + "&&mid=" + g.getMid() + "'>取消锁定</a>");

                out.println("<a href='PingServlet?name=" + g.getName() + "&&people=" + g.getPeople() + "&&money=" + g.getMoney() + "'>评价信息</a>");
                out.println("</td>");
                out.println("</tr>");
            }

            out.println("</table>");

//            out.println("<br/><br/><hr/><img src='images/bottom.png' width='100%'>");
//            out.println("</BODY>");
//
//            out.println("</HTML>");
            RequestDispatcher headerDispatcher1 = request.getRequestDispatcher("/WEB-INF/jsp/footer.jsp");
            headerDispatcher1.include(request, response);
            out.flush();
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @RequestMapping("/TuiServlet")
    public void TuiServlet(HttpServletRequest request, HttpServletResponse response) {
        try {
            response.setContentType("text/html;charset=utf-8");
            request.setCharacterEncoding("utf-8");

            int did = Integer.parseInt(request.getParameter("did"));
            int mid = Integer.parseInt(request.getParameter("mid"));

            //�ͷ�
            messageService.updateAFlag(mid);
            //ɾ������
            iDingdanService.delete(did);

            response.sendRedirect("MyDingdanServlet");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}